package com.woniu.immunologicalmechanism.service.impl;

import com.woniu.common.entity.OrderInfo;
import com.woniu.common.entity.ResponseResult;
import com.woniu.common.entity.User;
import com.woniu.immunologicalmechanism.feign.UserOpenfeign;
import com.woniu.immunologicalmechanism.mapper.ImmuneMapper;
import com.woniu.immunologicalmechanism.service.ImmuneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ImmuneServiceImpl implements ImmuneService {
   @Autowired
   ImmuneMapper immuneMapper;
    @Autowired
    ImmuneService immuneService;
    @Autowired
    UserOpenfeign userOpenfeign;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    @Override
    public List<OrderInfo> getorderBytel(String tel) {

        return  immuneMapper.selectdogBytel(tel);
    }

    @Override
    public List<OrderInfo> getorderinfosBystatus(int status) {

        return immuneMapper.selectdogBystatus(status);
    }


    //添加犬只信息
    @Override
    public ResponseResult addorderInfo(OrderInfo orderInfo) {
        ResponseResult result=new ResponseResult();
        try {
            immuneMapper.insertorderInfo(orderInfo);
            ResultUtil.insertOk(result);
        }
        catch (Exception e){
            e.printStackTrace();
            ResultUtil.error(result);
        }
        return result;
    }
    //修改核验信息状态
    @Override

    public ResponseResult   updateorderInfostatus(int oid,int status,String cause){
        ResponseResult result=new ResponseResult();
        try {
            immuneMapper.updateorderInfostatus(oid,status,cause);
            ResultUtil.updateOk(result);

        }
        catch (Exception e){
            e.printStackTrace();
            ResultUtil.error(result);
        }

        return result;
    }

    //核验id查询核验信息
    @Override
    public ResponseResult<OrderInfo> getdogByoid(int oid) {

        ResponseResult result=new ResponseResult();

        try {
            OrderInfo orderInfo = immuneMapper.selectdogByoid(oid);
            result.setData(orderInfo);
            ResultUtil.selectOk(result);
        }
        catch (Exception e){
            e.printStackTrace();
            ResultUtil.error(result);
        }
        return result;
    }
    //核验uid查询核验信息

    @Override
    public ResponseResult<List<OrderInfo>> getdogByUid(int uid) {

        ResponseResult result=new ResponseResult();

        try {
            List<OrderInfo> orderInfos = immuneMapper.selectdogByUid(uid);
                result.setData(orderInfos);
                ResultUtil.selectOk(result);
            if (orderInfos.size()==0){
                result.setMessage("不存在");
            }

        }
        catch (Exception e){
            e.printStackTrace();
            ResultUtil.error(result);
        }
        return result;
    }

    //更新redis缓存
   // @CachePut(value ="orderinfos",key = "#status")
    public ResponseResult   update(int status){
        ResponseResult result=new ResponseResult();
       result.setMessage("查询成功");
       result.setCode(200);
        List<OrderInfo> orderInfos = null;
       if(status==1){
           //status=1跟新核验的信息
           List<OrderInfo> orderInfos1 = immuneService.getorderinfosBystatus(1);
           orderInfos = immuneService.getorderinfosBystatus(2);
           orderInfos.addAll(orderInfos1);
           for (int i = 0; i < orderInfos.size(); i++) {
               User user = userOpenfeign.findUser(orderInfos.get(i).getUid());
               User user1 = (User) user;
               orderInfos.get(i).setUser(user);
           }

       }
        if(status==0){
     //status=1跟新未核验的信息
            orderInfos = immuneService.getorderinfosBystatus(0);
            for (int i = 0; i < orderInfos.size(); i++) {
                User user = userOpenfeign.findUser(orderInfos.get(i).getUid());
                User user1 = (User) user;
                orderInfos.get(i).setUser(user);
            }
        }
        result.setData(orderInfos);
        if(orderInfos.size()==0){
            result.setMessage("空空如也");
        }
        //System.out.println(result);
       // 更新缓存
        ValueOperations<String, Object> opsForValue =
                redisTemplate.opsForValue();
        opsForValue.set("orderinfos::"+status,result);
        return result;
    }




}
